package com.ht.controller;

import com.ht.bean.*;
import com.alibaba.fastjson.JSON;
import com.ht.common.FileUpload;
import com.ht.service.FeedbackService;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/make")
public class FeedbackController {
    @Resource
    private FeedbackService is;

    //去查看反馈列表
    @RequestMapping("/list")
    public String List(HttpSession session) {
        session.setAttribute("feedBackType","2");
        session.setAttribute("empId","1");
        session.setAttribute("stuId","1");
        session.setAttribute("depId","1");
        session.setAttribute("status","1");
//        session.setAttribute("userId","刘飞");
//        session.setAttribute("opinion","暂无");
        return "fklist";
    }

    //查询反馈表
    @RequestMapping("/fklist")
    @ResponseBody
    public Map list(Integer page,Integer limit,Feedback fk){
        System.out.println(page+""+limit);
        List<Map> list = is.alllist((page-1)*limit,limit,fk);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",is.countfk(fk));//总页数
        map.put("data",list);//数据
        return map;
    }

    //新增
    @RequestMapping("/toadd")
    public String toadd(Model model,Integer depid,String depName) {
        List<Dep> list = is.alldep(depid,depName);
        model.addAttribute("depList",list);
        return "fkadd";

    }

    //去新增
    @RequestMapping("/add")
    @ResponseBody
    public Map add(Feedback feedback,Emp emp, @RequestParam("fileface") MultipartFile fileface, HttpSession session) {
        String filePath = FileUpload.upload(fileface,session,"face");
        if(filePath!=null){
            feedback.setImage(filePath);
        }
        feedback.setFeedbackTime(new Date().toLocaleString());
        feedback.setReplyTime(new Date().toLocaleString());

        //从公共域里面取出值
        Integer feedBackType = Integer.parseInt((String) session.getAttribute("feedBackType"));
        String empId = (String)session.getAttribute("empId");
        String empname = (String)session.getAttribute("empname");
//        String userId = (String)session.getAttribute("userId");
//        String opinion = (String)session.getAttribute("opinion");

        feedback.setFeedBackType(feedBackType);
        feedback.setEmpId(empId);
        emp.setEmpName(empname);
        feedback.setStatus(1);
//        feedback.setUserId(userId);
//        feedback.setOpinion(opinion);




        Map map = new HashMap();
        if (is.addfk(feedback)) {
            map.put("msg", "添加成功");
        } else {
            map.put("msg", "添加失败!");
        }
        System.out.println("数据" + map);
        return map;
    }
    //查询查阅
    @RequestMapping("/chayue/{feedbackId}")
    public String chayue(Model model, @PathVariable("feedbackId") Integer feedbackId){
        Map list = is.allcheck(feedbackId);
        model.addAttribute("li",list);
        return "fkchecklist";
    }
    //查询
    @RequestMapping("/fk1list")
    @ResponseBody
    public Map list(){

        Map map = new HashMap();
        return map;
    }

    //审批
    @RequestMapping("/sp/{feedbackId}")
    public String sp(@PathVariable("feedbackId")Integer feedbackId,Model model,Feedback feedback){


        List<Feedback> Feedbacks = is.fkid(feedbackId);
        model.addAttribute("feedbackId",feedbackId);
        model.addAttribute("feedback",JSON.toJSON(Feedbacks.get(0)));

        return "fkupd";
    }

    //去审批
    @RequestMapping("/qsp")
    @ResponseBody
    public Map qsp(Feedback feedback){
        //审核时默认为已审核
        feedback.setStatus(2);
        Map map = new HashMap();
        if (is.sp(feedback)){
            map.put("msg","修改成功");
        }else {
            map.put("msg","修改失败");
        }
        return map;
    }

}
